package co.fitstart.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.admin.service.RoomScheduleDataService;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.site.RoomScheduleRecord;

@Service("roomScheduleDataService")
public class RoomScheduleDataServiceImpl implements RoomScheduleDataService{
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.RoomScheduleDataService#createRoomSchedule()
     */
    @SuppressWarnings("unchecked")
    public void createRoomSchedule() {
        List<CourseSchedule> courseSchedules = genericDaoSupport.searchForList("FROM CourseSchedule WHERE disabled = false AND scheduleStatus = 1 AND roomSchedule IS NULL");
        
        for(CourseSchedule courseSchedule : courseSchedules) {
            RoomScheduleRecord roomScheduleRecord = new RoomScheduleRecord(courseSchedule.getSite(),
                                                                           courseSchedule.getCourseRound().getCode(),
                                                                           courseSchedule.getOccurDate(),
                                                                           courseSchedule.getOccurHour(),
                                                                           courseSchedule.getRoomType(),
                                                                           1);
            genericDaoSupport.save(roomScheduleRecord);
            genericDaoSupport.update(courseSchedule.roomScheduleRecord(roomScheduleRecord));
        }
        
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.RoomScheduleDataService#updateRoomSchedule()
     */
    @SuppressWarnings("unchecked")
    public void updateRoomSchedule() {
        List<CourseSchedule> courseSchedules = genericDaoSupport.searchForList("FROM CourseSchedule WHERE disabled = false AND scheduleStatus = 1 AND roomSchedule IS NOT NULL");
        
        for(CourseSchedule courseSchedule : courseSchedules) {
            RoomScheduleRecord roomScheduleRecord = courseSchedule.getRoomSchedule();
            genericDaoSupport.update(roomScheduleRecord.update(courseSchedule));
        }
    }

}
